package com.redsoft.spirit.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.redsoft.spirit.service.AccountService;

@RestController
public class ConsumerController {

	// @Autowired
	// ConsumerClient consumerClient;

	@Autowired
	private AccountService userService;

	private Gson gson = new Gson();

	// @RequestMapping("/testForConsumer")
	// public String testForConsumer(String str) {
	// System.out.println(str + "我是一个来测试生产者的消费者");
	// return consumerClient.testForProductor("ang");
	// }

	@RequestMapping("/testForProductor")
	public String testForProductor(String str) {
		System.out.println(str + "我是一个来测试消费者的生产者。我同时也是个消费者。");
		return "我是一个来测试消费者的生产者。现在我同时也是个消费者";
	}

	@RequestMapping("/testPage")
	public String testPage() {
		return gson.toJson(userService.findUsers());
	}

	// @Autowired
	// private Sender sender;
	//
	// @RequestMapping("/testMq")
	// public void testMq(String str) {
	// System.out.println("测试mq");
	// sender.send1();
	// sender.send2();
	// }

	@Autowired
	RedisConnectionFactory connectionFactory;

	@RequestMapping("/testGetUser")
	public String testGetUser(String username) {

		// RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
		// redisTemplate.setConnectionFactory(connectionFactory);
		// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new
		// Jackson2JsonRedisSerializer(Object.class);
		// ObjectMapper om = new ObjectMapper();
		// om.setVisibility(PropertyAccessor.ALL,
		// JsonAutoDetect.Visibility.ANY);
		// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		// jackson2JsonRedisSerializer.setObjectMapper(om);
		// redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
		// redisTemplate.afterPropertiesSet();

		// RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
		// redisTemplate.setConnectionFactory(connectionFactory);
		// MsgPackRedisSerializer redisSerializer = new
		// MsgPackRedisSerializer(Object.class);
		//
		// ObjectMapper om = new ObjectMapper();
		// om.setVisibility(PropertyAccessor.ALL,
		// JsonAutoDetect.Visibility.ANY);
		// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		// redisSerializer.setObjectMapper(om);
		//
		// redisTemplate.setKeySerializer(redisSerializer);
		// redisTemplate.setHashKeySerializer(redisSerializer);
		// redisTemplate.setValueSerializer(redisSerializer);
		// redisTemplate.setHashValueSerializer(redisSerializer);
		// redisTemplate.afterPropertiesSet();

		// User user1 = new User();
		// user1.setUsername("user1");
		// user1.setId(12234);
		// user1.setImage("23423523453654758uhsddhfgbvdeertfjgfdsfdhfgnbfdfggfhdsdfghfgdgfdhggsfddfg");
		// user1.setPassword("234sfdfghgfds8fdikyt4we8gdimku45y4twegrodk64y53t4wiegrkdg");
		// user1.setEnabled("46574534dffhgsfdgfn");
		//
		// ValueOperations<String, Object> ops = redisTemplate.opsForValue();
		//
		// String key1 = "users/user1";
		// User user11 = null;
		//
		// long begin = System.currentTimeMillis();
		// for (int i = 0; i < 10000; i++) {
		// ops.set(key1 + i,user1);
		// user11 = (User)ops.get(key1 + i);
		// }
		//
		// long time = System.currentTimeMillis() - begin;
		// System.out.println("MsgPackRedisSerializer time:"+time);
		// return "111";

		return gson.toJson(userService.findUsers(username));
		// return gson.toJson(userRepository.findUser(username));
	}

}